package coletaDados;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class ConvertGrafemaFonema {
	private Map<String, List<String>> mapStatus;
	private Set<String> dictionary = new TreeSet<String>();
	private static final String[] comando = {"bin" + File.separator + "ConversorG2P" + File.separator + "G2P.exe",
		"palavras.txt" , "false"}; 

	public ConvertGrafemaFonema(Map<String, List<String>> map) {
		this.mapStatus = map;
	}

	public void ConvertSave() {
		for (String u : (new ArrayList<String>(mapStatus.keySet()))) {
			dictionary.addAll(mapStatus.get(u));
		}
		salvarDict();
		convert();
	}

	private void convert() {
		try {
			ProcessBuilder pb = new ProcessBuilder(comando);
			Process p = pb.directory(new File("bin/ConversorG2P")).start();
			Scanner s = new Scanner(p.getInputStream());
			while (s.hasNextLine()) {
				s.nextLine();
			}
			p.exitValue();
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void salvarDict() {
		try {
			PrintWriter printer = new PrintWriter(new File("bin" + File.separator +"ConversorG2P" + File.separator,
					"palavras.txt"));
			for(String s : (new ArrayList<String>(dictionary))){
				printer.write(s + "\n");
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
